<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class StatisticsAttendScore extends AdminController {

	function __construct() { 
		parent::__construct();
		$this -> load -> model("Attend_teaching_content_model");
		$this -> load -> model("Attend_teaching_implementation_model");
		$this -> load -> model("Attend_student_status_model");
		$this -> load -> model("Admin_model");
	}

	public function Index() {
		$this ->load->model('Admin_model');
		$result['dept_list']=$this ->Admin_model->query('select distinct dept_id,dept_name from db_admin where dept_id is not null');
		$this -> load -> view('admin/statisticsAttendScoreList',$result);
	}

	function getDatas($params = array(), $isExport = false) {
		 $where='where 1=1';
		$params= $_POST;
		if(isset($params['dept_name']) && $params['dept_name']!=-1){
			$where.=$this ->sqlEscape(' and dept_name=??',array($params['dept_name']));
			$dept_name=$params['dept_name'];
		}
		if (isset($params['start_time']) && isset($params['end_time'])) {
			$where.=$this->sqlEscape(' and db_attend.created>?? and db_attend.created<??',array(strtotime($params['start_time']),strtotime($params['end_time'])));
		}else if (isset($_POST['start_time']) && isset($_POST['end_time'])) {
			$where.=$this->sqlEscape(' and db_attend.created>?? and db_attend.created<??',array(strtotime($_POST['start_time']),strtotime($_POST['end_time'])));
		}
		if(!$isExport){
			if(isset($params['pageNo']) && isset($params['pageSize'])){			
				$pageNo=$_POST['pageNo']-1;
				$pageSize=$_POST['pageSize'];
			}else{
				$pageNo='0';
				$pageSize='20';
			}
		}

		// $sql='select * from (SELECT * from db_attend_teaching_content UNION all SELECT * from db_attend_teaching_implementation UNION all SELECT * from db_attend_student_status) attend ';
		if(!$isExport){			
			$data['ret']=$this -> Attend_teaching_content_model -> query('select attend.type,key_index from (SELECT * from db_attend_teaching_content  where attend_id > 910 UNION all SELECT * from db_attend_teaching_implementation  where attend_id > 910 UNION all SELECT * from db_attend_student_status  where attend_id > 910) attend left join db_attend on db_attend.id=attend.attend_id  '.$where.' group by list_id limit '.$pageSize*$pageNo.','.$pageSize);
			$count=$this -> Attend_teaching_content_model -> query('select count(*) as count from (SELECT * from db_attend_teaching_content where attend_id > 910  UNION all SELECT * from db_attend_teaching_implementation where attend_id > 910  UNION all SELECT * from db_attend_student_status where attend_id > 910 ) attend left join db_attend on db_attend.id=attend.attend_id '.$where);
			$data['count']=1;
			$data['totalPage']=ceil($data['count']/$pageSize);
		}else{
			$data['ret']=$this -> Attend_teaching_content_model -> query('select attend.type,key_index  from (SELECT * from db_attend_teaching_content where attend_id > 910  UNION all SELECT * from db_attend_teaching_implementation  where attend_id > 910 UNION all SELECT * from db_attend_student_status where attend_id > 910 ) attend left join db_attend on db_attend.id=attend.attend_id  '.$where.' group by list_id ');
			$count=$this -> Attend_teaching_content_model -> query('select count(*) as count from (SELECT * from db_attend_teaching_content where attend_id > 910  UNION all SELECT * from db_attend_teaching_implementation where attend_id > 910  UNION all SELECT * from db_attend_student_status where attend_id > 910 ) attend left join db_attend on db_attend.id=attend.attend_id '.$where);			
		}
		
		
		if(isset($_POST['pageNo'])){
			$data['pageNo']=$_POST['pageNo'];
		}
		
		$avg=$this->Attend_teaching_content_model->query('select round(avg(get_score),2) avg from (SELECT * from db_attend_teaching_content where attend_id > 910  UNION all SELECT * from db_attend_teaching_implementation where attend_id > 910  UNION all SELECT * from db_attend_student_status where attend_id > 910 ) attend  left join db_attend on db_attend.id=attend.attend_id '.$where.' GROUP  BY list_id');
		$max=$this->Attend_teaching_content_model->query('select cast(max(get_score) as signed) max from (SELECT * from db_attend_teaching_content where attend_id > 910  UNION all SELECT * from db_attend_teaching_implementation where attend_id > 910  UNION all SELECT * from db_attend_student_status where attend_id > 910 ) attend left join db_attend on db_attend.id=attend.attend_id  '.$where.' GROUP BY list_id');
		$min=$this->Attend_teaching_content_model->query('select cast(min(get_score) as signed) min from (SELECT * from db_attend_teaching_content where attend_id > 910  UNION all SELECT * from db_attend_teaching_implementation where attend_id > 910  UNION all SELECT * from db_attend_student_status where attend_id > 910 ) attend  left join db_attend on db_attend.id=attend.attend_id '.$where.' GROUP  BY list_id');
		// var_dump($avg);
		foreach ($data['ret'] as $k => $v) {
			$data['ret'][$k]['type']=(new TeachingType)->show($v['type']);

			$data['ret'][$k]['max']=$max[$k]['max'];			
			$data['ret'][$k]['min']=$min[$k]['min'];
			$data['ret'][$k]['avg']=$avg[$k]['avg'];
			// $data['ret'][$k]['avg']=sprintf("%.2f",$avg[$k]['avg']);			
		}
		// exit(retJson('返回反馈列表',true,$result));
		// echo json_encode($data);

		// if (!$isExport) {
		// 	$data = $this -> Student_feedback_model -> get_page_list($connar);
		// } else {
		// 	$data["ret"] = $this -> Student_feedback_model -> get_list_full($connar);
		// }
		
		if (!$isExport) {
			echo json_encode($data);
		} else {
			// return $data["ret"];
			$this-> load ->library('PHPExcel/PHPExcel');
	        $excel = new PHPExcel();
	        //公共样式
	        $excel->getActiveSheet()->getDefaultStyle()->getFont()->setSize(11); 
	        $excel->getActiveSheet()->getDefaultStyle()->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
	        $excel->getActiveSheet()->getDefaultStyle()->getAlignment()->setWrapText(true);
	        $excel->getActiveSheet()->getDefaultColumnDimension()->setWidth(23);
	        $excel->getActiveSheet()->getDefaultStyle()->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
	        $excel->getActiveSheet()->getRowDimension('1')->setRowHeight(20);

	        $x_line=0;
	        $y_line=1;
	        $excel->getActiveSheet()->getStyle('A1')->getFont()->setBold(true);      //第一行是否加粗
	        $excel->getActiveSheet()->getStyle('A1')->getFont()->setSize(16);         //第一行字体大小
	        $excel->getActiveSheet()->getRowDimension('1')->setRowHeight(30);    //第一行行高
	        // 合并单元格
	        $excel->getActiveSheet()->mergeCells('A1:E1');
	        if(isset($dept_name)){
	        	$excel->setActiveSheetIndex(0)->setCellValue('A1', $dept_name.'听课表加分明细统计');
	        }else{
	        	$excel->setActiveSheetIndex(0)->setCellValue('A1', '听课表加分明细统计');
	        }
	        
			$y_line=2;
			//标题
	        $title=array('评价项目','评价内容','最高分','最低分','平均分');
	        foreach ($title as $k => $v) {
	        	$excel->getActiveSheet()->setCellValue(PHPExcel_Cell::stringFromColumnIndex($x_line).$y_line,$v);
	        	$excel->getActiveSheet()->getStyle(PHPExcel_Cell::stringFromColumnIndex($x_line).$y_line)->getFont()->setBold(true);   
	        	$x_line++;
	        }

	        $x_line=0;
	        $y_line=3;
	        //正式数据
	        
	        foreach ($data['ret'] as $k => $v) {        	
	        	foreach ($v as $k1 => $v1) {	        		
	        		$excel->getActiveSheet()->setCellValue(PHPExcel_Cell::stringFromColumnIndex($x_line).$y_line,$v1);	        		
	        		$x_line++;
	        	}
	        	$x_line=0;
	        	$y_line++;
	        }
	        $write = new PHPExcel_Writer_Excel2007($excel); 
	       	$director = "upload/export/" . date('Ymd', time()) . "/";
	        if (!file_exists($director)) {
	            //检查是否有该文件夹，如果没有就创建，并给予最高权限
	            mkdir($director, 0777);
	        }        
			$file_name ='听课表加分明细统计'.date('Ymd').'.xlsx';       	
	       	$write->save($director.iconv('utf-8', 'gb2312', $file_name));
			exit(retJson('返回地址',true,$director.$file_name));
		}

	}


	
}